package com.mm.controller;

import com.mm.domain.User;
import com.mm.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 不同类型的接口,用于外部调用测试
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private Registration registration;//老版本用DiscoveryClient
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/getString")
    public String getString() throws InterruptedException {
//        Thread.sleep(5000l);//测试RestTemplate调用超时的时候用到
        System.out.println("ServiceId:" + registration.getServiceId()
                + ",Host:" + registration.getHost()
                + ",port" + registration.getPort());
        return "mm_getString";
    }

    @GetMapping("/get_String1")
    public String get_String1(){
        return "get_String1";
    }
    @GetMapping("/get_String2")
    public String get_String2(@RequestParam("name") String name){
        return "get_String2_" + name;
    }
    @GetMapping("/get_User")
    public User get_User(){
        User user = new User();
        user.setUsername("mm");
        return user;
    }
    @GetMapping("/get_List")
    public List<User> get_List(){
        return userRepository.findAll();
    }

    @PreAuthorize("hasAnyAuthority('resource1','resource2')")
    @GetMapping("/resource1")
    public String resource1(){
        return "resource1";
    }

    @PreAuthorize("hasAnyAuthority('resource3')")
    @GetMapping("/resource3")
    public String resource3(){
        return "resource3";
    }
}
